/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { isArray } from 'lodash';
import moment from 'moment';
import type { ApproveDataType, TimeParamsType } from '../types/Common';

// 获取随机字符
export const getRanString = (num: number = 5) => {
  const arr = [];
  for (let i = 0; i < num; i++) {
    arr.push(String.fromCharCode(65 + Math.ceil(Math.random() * 25)));
  }
  return arr.join('');
};

export const platFormType = {
  kdy: '快递员',
  yz: '驿站',
  open: '开放平台',
};

// 通过模版和拒绝模版通用
export const TemApproveCoulmns = [
  {
    title: '申请人',
    dataIndex: 'cm_phone',
  },
  {
    title: '平台标识',
    dataIndex: 'user_type',
  },
  {
    title: '模版内容',
    dataIndex: 'content',
  },
  {
    title: '申请日期',
    dataIndex: 'apply_time',
  },
  {
    title: '审核日期',
    dataIndex: 'approve_time',
  },
  {
    title: '审核人',
    dataIndex: 'approver',
  },
];

// 签名模块 已经过/已拒绝 共用
export const SignApproveColumns = [
  {
    title: '申请人',
    dataIndex: 'phone',
  },
  {
    title: '平台标识',
    dataIndex: 'source',
    render: (text: string) => platFormType[text],
  },
  {
    title: '签名内容',
    dataIndex: 'sign',
  },
  {
    title: '申请日期',
    dataIndex: 'create_time',
  },
  {
    title: '审核日期',
    dataIndex: 'approve_time',
  },
  {
    title: '审核人',
    dataIndex: 'operator',
  },
];

export const formatFormValue = (data: any) => {
  const { time, ...restparams } = data;
  const timeParams: TimeParamsType = {};
  if (isArray(time)) {
    timeParams.start_time = moment(time[0]).format('YYYY-MM-DD');
    timeParams.end_time = moment(time[1]).format('YYYY-MM-DD');
  }
  return { ...timeParams, ...restparams };
};

export const formatTemApproveParams = (data: ApproveDataType[]) => {
  return {
    ids: JSON.stringify(
      data.map((item: ApproveDataType) => {
        const { tid, kb_id, type } = item;
        return {
          tid,
          kb_id,
          type,
        };
      }),
    ),
  };
};
export const formatTemSvipApproveParams = (data: ApproveDataType[]) => {
  return {
    ids: data.map((item: ApproveDataType) => item.tid),
  };
};
export const formatTemBlackParams = (data: ApproveDataType) => {
  return {
    tplId: data.tid,
    courierPhone: data.cm_phone,
    cm_id: data.cm_app_id || data.cm_id,
    kb_id: data.kb_id,
  };
};

export const formatSignApproveParams = (data: ApproveDataType[]) => {
  return {
    arrData: data.map((item: ApproveDataType) => [item.ssid, item.kb_id]),
  };
};

export const formatSenitive = (data: ApproveDataType) => {
  const { content, sensitive_word = '' } = data;
  const reg = new RegExp(sensitive_word, 'gi');
  const newcontent = content.replace(reg, ` ${sensitive_word} `).split(' ');
  return newcontent;
};

export const modalColumns = (svipStatus: boolean) => {
  return svipStatus
    ? [
        {
          label: '营销内容含敏感词语',
          value: '1',
        },
        {
          label: '营销内容中含有广告',
          value: '2',
        },
        {
          label: '营销内容与快递无关',
          value: '3',
        },
      ]
    : [
        {
          label: '短信内容含敏感词语',
          value: '1',
        },
        {
          label: '短信内容中含有广告',
          value: '2',
        },
        {
          label: '短信内容与快递无关',
          value: '3',
        },
      ];
};
